home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 112 / EnigmaAmiga112CD.iso / dalla rivista / giochi in rete / sorgenti_amislate / palette.c < prev    next >
C/C++ Source or Header  |  1995-08-21  |  14KB  |  528 lines

  1. #include <stdio.h>
  2. #include <intuition/intuition.h>
  3. #include <intuition/intuitionbase.h>
  4. #include <exec/types.h>
  5. #include <libraries/dos.h>            /* contains RETURN_OK, RETURN_WARN #def's */
  6. #include <clib/exec_protos.h>
  7. #include <clib/intuition_protos.h>
  8. #include <clib/graphics_protos.h>
  9. #include <graphics/gfxbase.h>
  10.  
  11. #include "ToolBox.h"
  12. #include "AmiSlate.h"
  13. #include "drawlang.h"
  14. #include "drawrexx.h"
  15. #include "drawrexx_aux.h"
  16. #include "tools.h"
  17. #include "flood.h"
  18. #include "remote.h"
  19. #include "palette.h"
  20.  
  21. __chip extern unsigned short toolbox_h_image[];  /* hires version */
  22. __chip extern unsigned short toolbox_hs_image[]; /* selected */
  23. extern int XPos, YPos;
  24. extern struct Screen *Scr;
  25. extern struct PaintInfo PState;
  26. extern struct Window *DrawWindow;
  27. extern int nSynchPaletteMode;
  28.  
  29. int nPaletteColumns;
  30. struct BitMap ToolBoxHBitMap,ToolBoxHSBitMap;
  31.  
  32. /* palette.c -- routines for displaying and manipulating the toolbox
  33.    and the palette */
  34.  
  35. BOOL ChangeDepth(int nNewDepth)
  36. {    
  37.     if ((nNewDepth > Scr->RastPort.BitMap->Depth)||(nNewDepth < 1)) return(FALSE);
  38.             
  39.     PState.ubDepth = nNewDepth;
  40.  
  41.     /* This will adjust the palette to the proper # of colors */
  42.     EraseToolBox(DrawWindow->Width,DrawWindow->Height);
  43.     DrawToolBox();
  44.  
  45.     return(TRUE);
  46. }
  47.  
  48.  
  49.  
  50. void InitToolBox(void)
  51. {
  52.     int i;
  53.     
  54.     InitBitMap(&ToolBoxHBitMap, TOOLBOXH_DEPTH, TOOLBOXH_WIDTH, TOOLBOXH_HEIGHT);
  55.     InitBitMap(&ToolBoxHSBitMap, TOOLBOXH_DEPTH, TOOLBOXH_WIDTH, TOOLBOXH_HEIGHT);
  56.     
  57.     for (i=0;i<ToolBoxHBitMap.Depth;i++)
  58.     {
  59.         ToolBoxHBitMap.Planes[i]   = 
  60.                 (PLANEPTR) &toolbox_h_image[i*(TOOLBOXH_BYTESPERROW*TOOLBOXH_ROWS)/2];
  61.         ToolBoxHSBitMap.Planes[i]  =
  62.                 (PLANEPTR) &toolbox_hs_image[i*(TOOLBOXH_BYTESPERROW*TOOLBOXH_ROWS)/2];
  63.  
  64.     }
  65.     return;
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /* Draws a palette square with the given specifications.  If nRev is TRUE,
  73.    it will make it selected */
  74. static void DrawPaletteSquare(int nX, int nY, int nHeight, int nWidth, int nColor, BOOL BRev)
  75. {
  76.     int nBlack=1, nWhite=2;
  77.  
  78.     SetAPen(DrawWindow->RPort,nColor);
  79.     RectFill(DrawWindow->RPort,nX+1,nY, nX + nWidth-1, nY + nHeight);
  80.         
  81.     if (BRev == TRUE) {nBlack = 2; nWhite = 1;}
  82.         
  83.     /* Draw outline for colorsquare */
  84.     if ((nGetPaletteSquareWidth() > 3) && (nGetPaletteSquareHeight() > 3))
  85.     {    
  86.         SetAPen(DrawWindow->RPort, nBlack);    /* black */
  87.         Move(DrawWindow->RPort, nX + nWidth - 1, nY + nHeight);
  88.         Draw(DrawWindow->RPort, nX + nWidth - 1, nY);
  89.         Move(DrawWindow->RPort, nX + nWidth - 1, nY + nHeight);
  90.         Draw(DrawWindow->RPort, nX,              nY + nHeight);
  91.     }
  92.     
  93.     if ((nGetPaletteSquareWidth() > 7)    && (nGetPaletteSquareHeight() > 7))
  94.     {
  95.         Move(DrawWindow->RPort, nX + nWidth - 2, nY + nHeight - 1);
  96.         Draw(DrawWindow->RPort, nX + nWidth - 2, nY + 1);
  97.         Move(DrawWindow->RPort, nX + nWidth - 2, nY + nHeight - 1);
  98.         Draw(DrawWindow->RPort, nX + 1,          nY + nHeight - 1);
  99.  
  100.     }    
  101.  
  102.     if ((nGetPaletteSquareWidth() > 3) && (nGetPaletteSquareHeight() > 3))
  103.     {                
  104.         SetAPen(DrawWindow->RPort, nWhite);
  105.         Move(DrawWindow->RPort, nX,              nY);
  106.         Draw(DrawWindow->RPort, nX + nWidth - 1, nY);
  107.         Move(DrawWindow->RPort, nX,              nY);
  108.         Draw(DrawWindow->RPort, nX,              nY + nHeight);
  109.     }
  110.     
  111.     if ((nGetPaletteSquareWidth() > 7)    && (nGetPaletteSquareHeight() > 7))
  112.     {
  113.         Move(DrawWindow->RPort, nX + 1,          nY + 1);
  114.         Draw(DrawWindow->RPort, nX + nWidth - 2, nY + 1);
  115.         Move(DrawWindow->RPort, nX + 1,          nY + 1);
  116.         Draw(DrawWindow->RPort, nX + 1,          nY + nHeight - 1);
  117.     }
  118.     
  119.     return;
  120. }
  121.  
  122.  
  123.  
  124.  
  125.     
  126.  
  127.  
  128. static int nGetPaletteHeight(void)
  129. {
  130.     return(DrawWindow->Height - DrawWindow->BorderBottom - 
  131.                (PState.nToolBoxHeight - Scr->WBorTop + Scr->RastPort.TxHeight) - 6);
  132. }
  133.  
  134. static int nGetNumberOfColors(void)
  135. {
  136.    int nNumberOfColors = 1, nCount = PState.ubDepth;
  137.    
  138.    /* Count number of colors in palette */
  139.     nCount = PState.ubDepth;
  140.     while (nCount > 0)
  141.     {
  142.         nNumberOfColors *= 2;
  143.         nCount--;
  144.     }        
  145.     return(nNumberOfColors);
  146. }
  147.     
  148. int nGetGadgetHeight(void)
  149. {
  150.     return(PState.nToolBoxHeight/4);
  151. }
  152.  
  153. int nGetGadgetWidth(void)
  154. {
  155.     return(PState.nToolBoxWidth/2);
  156. }
  157.  
  158. static int nGetPaletteSquareHeight(void)
  159. {
  160.     int nTemp = nGetNumberOfColors();
  161.     
  162.     /* Do we have an even number of rows? */
  163.     while (nTemp > 0) nTemp -= nPaletteColumns;
  164.     
  165.     if (nTemp == 0) return(nGetPaletteHeight() / (nGetNumberOfColors()/nPaletteColumns));    
  166.         
  167.     nTemp = (nGetNumberOfColors()+nPaletteColumns);
  168.     if (nTemp < 0) nTemp = nGetNumberOfColors();
  169.     
  170.     /* If not, make sure we are one row shorter. */
  171.     return(nGetPaletteHeight() / (nTemp/nPaletteColumns));    
  172. }
  173.     
  174. static int nGetPaletteSquareWidth(void)
  175. {
  176.     return(PState.nToolBoxWidth / nPaletteColumns); 
  177. }
  178.  
  179. void DrawToolBox(void)
  180. {
  181.     int nToolBoxLeft = nGetToolBoxLeft(); 
  182.     int nCount, nCurrentX, nCurrentY = DrawWindow->BorderTop;
  183.         int nNumberOfColors = nGetNumberOfColors(); 
  184.  
  185.     /* default:  try... */
  186.     nPaletteColumns = 1;
  187.  
  188.     /* Try to get a good fit on palette columns */
  189.     if (nGetPaletteSquareHeight() < 25) nPaletteColumns++;
  190.     while ((nGetPaletteSquareHeight() < 10)&&(nPaletteColumns < 16)) nPaletteColumns++;        
  191.     while ((nGetPaletteSquareWidth()  <  5)&&(nPaletteColumns >  1)) nPaletteColumns--;
  192.         
  193.     /* Draw ToolBox */
  194.     nCurrentX = nToolBoxLeft;
  195.     
  196.     BltBitMapRastPort(&ToolBoxHBitMap,0,0,DrawWindow->RPort,
  197.            nToolBoxLeft,    DrawWindow->BorderTop,
  198.            TOOLBOXH_WIDTH,TOOLBOXH_HEIGHT,0xC0);
  199.  
  200.     SetAPen(DrawWindow->RPort,1); 
  201.     Move(DrawWindow->RPort,nToolBoxLeft-1,DrawWindow->BorderTop+1);
  202.     Draw(DrawWindow->RPort,nToolBoxLeft-1,DrawWindow->Height - DrawWindow->BorderBottom - 1);
  203.  
  204.      /* DeSelect Current Mode */
  205.     nCount = (PState.uwMode & ~(MODE_FILLED)) - 1;
  206.     
  207.     while (nCount >= 2)
  208.     {
  209.         nCount -= 2;    
  210.         nCurrentY += nGetGadgetHeight();
  211.     }
  212.     if (nCount == 1) nCurrentX += nGetGadgetWidth();
  213.     
  214.     /* Select Selected Tool Square */
  215.     BltBitMapRastPort(&ToolBoxHSBitMap,nCurrentX-nToolBoxLeft, 
  216.                             nCurrentY-DrawWindow->BorderTop, 
  217.                             DrawWindow->RPort, nCurrentX, nCurrentY, 
  218.                             nGetGadgetWidth()-1, 
  219.                             nGetGadgetHeight()-1, 0xC0);    
  220.  
  221.     SetDrMd(DrawWindow->RPort,JAM1);
  222.  
  223.     /* If we selected circle or square, figure out whether or not
  224.        they wanted it filled in */
  225.     if (PState.uwMode == (MODE_SQUARE|MODE_FILLED)) Rectangle(nCurrentX+5,nCurrentY+5, nCurrentX+17, nCurrentY+17, TRUE);
  226.     if (PState.uwMode == (MODE_CIRCLE|MODE_FILLED)) Rectangle(nCurrentX+5,nCurrentY+5, nCurrentX+17, nCurrentY+17, TRUE);
  227.  
  228.     /* Don't try to draw Palette if there's no room for it */
  229.     if (nGetPaletteHeight() <= 0) return;
  230.     
  231.     /* ---Now draw Palette--- */
  232.     nCurrentY = DrawWindow->BorderTop + PState.nToolBoxHeight;
  233.  
  234.     for (nCount = 0; nCount < nNumberOfColors; nCount++)
  235.         DrawPaletteEntry(nCount, (PState.uwFColor == nCount));
  236.  
  237.   return;
  238. }
  239.  
  240.  
  241.  
  242.  
  243. void EraseToolBox(int nWidth, int nHeight)
  244. {
  245.     int nOldToolBoxLeft;
  246.     int nWindowBottom;
  247.     
  248.     if (nWidth < 0) 
  249.     {
  250.            nWidth = PState.nDefaultWidth;
  251.            nHeight = PState.nDefaultHeight;
  252.     }
  253.     if (DrawWindow->Width < nWidth) nWidth = DrawWindow->Width;
  254.     nOldToolBoxLeft  = nWidth - DrawWindow->BorderRight - PState.nToolBoxWidth -1;  
  255.     if (DrawWindow->Height < nHeight) nHeight = DrawWindow->Height;
  256.     nWindowBottom = nHeight - DrawWindow->BorderBottom - 1;
  257.     
  258.     /* Erase ToolBox */
  259.     SetAPen(DrawWindow->RPort,PState.uwBColor);
  260.     Rectangle(nOldToolBoxLeft, DrawWindow->BorderTop, 
  261.                 nWidth-DrawWindow->BorderRight, nWindowBottom, TRUE);    
  262.     return;
  263. }
  264.  
  265.  
  266. int nGetToolBoxLeft(void)
  267. {
  268.     return(DrawWindow->Width - DrawWindow->BorderRight - PState.nToolBoxWidth); 
  269. }
  270.  
  271. int nGetToolBoxTop(void)
  272. {
  273.     return(DrawWindow->BorderTop);
  274. }
  275.  
  276.  
  277.  
  278. BOOL DrawPaletteEntry(int nColor, BOOL BRev)
  279. {
  280.     int nCount = nColor, nX, nY, nPaletteSquareHeight, nPaletteSquareWidth;
  281.     int nToolBoxLeft = nGetToolBoxLeft();
  282.     
  283.     if ((nColor<0)||(nColor >= nGetNumberOfColors())) return(FALSE);
  284.             
  285.     nPaletteSquareHeight = nGetPaletteSquareHeight();
  286.     if (nPaletteSquareHeight < 0) return(FALSE);
  287.     nPaletteSquareWidth  = nGetPaletteSquareWidth();
  288.     if (nPaletteSquareWidth < 0 ) return(FALSE);
  289.     
  290.     /* Now regenerate the co-ordinates for the color */
  291.     nY = DrawWindow->BorderTop + PState.nToolBoxHeight;
  292.     nX = nToolBoxLeft + 1;
  293.     while (nCount >= nPaletteColumns)
  294.     {
  295.         nCount -= nPaletteColumns;
  296.         nY += nPaletteSquareHeight;
  297.     }
  298.     nX += nCount * nPaletteSquareWidth;    
  299.     
  300.     DrawPaletteSquare(nX,nY, nPaletteSquareHeight, nPaletteSquareWidth, nColor, BRev);
  301.     return(TRUE);
  302. }        
  303.  
  304.  
  305.  
  306.  
  307. /* Given a tool index nIndex, set (nX, nY) to appropriate co-ordinates
  308.    (relative to the top left of the ToolBox to click that tool */
  309. void SetFakeToolBoxClick(int nIndex, int * nX, int * nY)
  310. {
  311.     int nTx = 5, nTy = 5, nHeight = nGetGadgetHeight();
  312.  
  313.     /* For selection of tools */    
  314.     if (nIndex < 20)
  315.     {
  316.         /* Filled circle = mode 13 */
  317.         if (nIndex == 13) 
  318.         {
  319.             nIndex = 3;    /* Circle */
  320.             nTx += 10;
  321.             nTy += 10;    /* Lower right side */
  322.         }
  323.         /* Filled circle = mode 14 */
  324.         if (nIndex == 14) 
  325.         {
  326.             nIndex = 4;    /* Square */
  327.             nTx += 10;
  328.             nTy += 10;    /* Lower right side */
  329.         }
  330.         
  331.         while (nIndex > 1)
  332.         {
  333.             nTy += nHeight;
  334.             nIndex -= 2;
  335.         }
  336.         
  337.         if (nIndex == 1) nTx += nGetGadgetWidth();
  338.  
  339.         *nX = nTx;
  340.         *nY = nTy;
  341.     }
  342.     else
  343.     {
  344.         /* For selection of palette; dummy result in palette */
  345.         *nX = 5;
  346.         *nY = nHeight * 4 + 3;
  347.     }
  348.     
  349.     return;
  350. }
  351.     
  352.     
  353.     
  354. /* If called with nOptForceToolIndex < 0, get the tool to press from
  355.    mouse position.  Otherwise, uses nOptForceToolIndex as index */
  356. void HandleToolBox(int nOptForceToolIndex)
  357. {
  358.     int nGadgetHeight = nGetGadgetHeight();
  359.     int nGadgetWidth  = nGetGadgetWidth();
  360.     int XSelectIndex = -2;
  361.     int nToolBoxLeft = nGetToolBoxLeft(); 
  362.     int nX = XPos - nToolBoxLeft, nY = YPos-DrawWindow->BorderTop;
  363.     int nHiLiteX = nToolBoxLeft, nHiLiteY = DrawWindow->BorderTop - nGadgetHeight;
  364.     int nOldX = nToolBoxLeft, nOldY = DrawWindow->BorderTop;
  365.     int nCount, nPaletteSquareHeight, nPaletteSquareWidth;
  366.     static LONG lToolIndex;
  367.     const static LONG lToolType = REXX_REPLY_TOOLSELECT, lColorType = REXX_REPLY_COLORSELECT;
  368.  
  369.     if (nOptForceToolIndex >= 0) SetFakeToolBoxClick(nOptForceToolIndex, &nX, &nY); 
  370.     
  371.     /* Figure out which Gadget was selected, if any.
  372.     
  373.         0    1
  374.         2    3
  375.         4    5
  376.         6    7
  377.     */
  378.  
  379.       while (nY > 0)
  380.       {
  381.           XSelectIndex += 2;
  382.           nY -= nGadgetHeight;
  383.           nHiLiteY += nGadgetHeight;
  384.       }     
  385.      if (nX > nGadgetWidth) {XSelectIndex++;  nHiLiteX += nGadgetWidth;}
  386.  
  387.      
  388.      if (XSelectIndex < 7)
  389.      {
  390.          /* DeSelect Current Mode */
  391.         nCount = (PState.uwMode & ~(MODE_FILLED)) - 1;
  392.         while (nCount >= 2)
  393.         {
  394.             nCount -= 2;    
  395.             nOldY += nGadgetHeight;
  396.         }
  397.         if (nCount == 1) nOldX += nGadgetWidth;
  398.         
  399.         /* DeSelect Old Tool Square */
  400.         BltBitMapRastPort(&ToolBoxHBitMap,nOldX - nToolBoxLeft, 
  401.                             nOldY - DrawWindow->BorderTop, 
  402.                             DrawWindow->RPort, nOldX, nOldY, 
  403.                             nGetGadgetWidth()-1, 
  404.                             nGetGadgetHeight()-1, 0xC0);    
  405.             
  406.         /* Now draw new selection */
  407.         BltBitMapRastPort(&ToolBoxHSBitMap,nHiLiteX - nToolBoxLeft, 
  408.                             nHiLiteY - DrawWindow->BorderTop, 
  409.                             DrawWindow->RPort, nHiLiteX, nHiLiteY, 
  410.                             nGetGadgetWidth()-1, 
  411.                             nGetGadgetHeight()-1, 0xC0);    
  412.  
  413.         PState.uwMode = XSelectIndex + 1;
  414.     
  415.         /* If we selected circle or square, figure out whether or not
  416.            they wanted it filled in */
  417.         if ((PState.uwMode == MODE_SQUARE)&&(nX > (-nY)))
  418.         {
  419.             /* Set Filled */
  420.             PState.uwMode |= MODE_FILLED;
  421.             SetAPen(DrawWindow->RPort, 1);
  422.             Rectangle(nHiLiteX+5,nHiLiteY+5, nHiLiteX+17, nHiLiteY+17, TRUE);
  423.         }
  424.         if ((PState.uwMode == MODE_CIRCLE)&&((nX - nGetGadgetWidth()) > (-nY)))
  425.         {
  426.             /* Set Filled */
  427.             PState.uwMode |= MODE_FILLED;
  428.             SetAPen(DrawWindow->RPort, 1);
  429.             Rectangle(nHiLiteX+5,nHiLiteY+5, nHiLiteX+17, nHiLiteY+17, TRUE);
  430.         }            
  431.             
  432.         /* get out of any polygons we were in */
  433.         ResetPolygonTool();
  434.         
  435.         /* Tell ARexx about if he wants to know */
  436.         if (PState.uwRexxWaitMask & REXX_REPLY_TOOLSELECT)
  437.         {
  438.             lToolIndex = XSelectIndex + (((PState.uwMode & MODE_FILLED) != 0) * 10);
  439.             ((struct rxd_waitevent *) *(&RexxState.array))->res.type    = &lToolType; 
  440.             ((struct rxd_waitevent *) *(&RexxState.array))->res.code1   = &lToolIndex;
  441.             SetStandardRexxReturns();
  442.         }
  443.         return;
  444.     }
  445.     
  446.     /* If we get here, we either have a clear command or a palette change! */
  447.     if (XSelectIndex == 7)
  448.     {        
  449.         if (PState.BDrawEnabled == TRUE)
  450.         {
  451.             SetAPen(DrawWindow->RPort,PState.uwBColor);     
  452.         
  453.             /* Select Square */
  454.             BltBitMapRastPort(&ToolBoxHSBitMap,nHiLiteX - nToolBoxLeft, 
  455.                                 nHiLiteY - DrawWindow->BorderTop, 
  456.                                 DrawWindow->RPort, nHiLiteX, nHiLiteY, 
  457.                                 nGetGadgetWidth()-1, 
  458.                                 nGetGadgetHeight()-1, 0xC0);    
  459.                                 
  460.             ClearWindow();
  461.     
  462.             /* DeSelect Square */
  463.             BltBitMapRastPort(&ToolBoxHBitMap,nHiLiteX - nToolBoxLeft, 
  464.                                 nHiLiteY - DrawWindow->BorderTop, 
  465.                                 DrawWindow->RPort, nHiLiteX, nHiLiteY, 
  466.                                 nGetGadgetWidth()-1, 
  467.                                 nGetGadgetHeight()-1, 0xC0);    
  468.     
  469.             OutputAction(FROM_IDCMP, COMMAND, COMMAND_CLEAR, NOP_PAD, NOP_PAD, NOP_PAD, DEST_PEER|DEST_FILE);
  470.         }
  471.  
  472.         /* Tell ARexx about if he wants to know */
  473.         if (PState.uwRexxWaitMask & REXX_REPLY_TOOLSELECT)
  474.         {
  475.             lToolIndex = XSelectIndex + (((PState.uwMode & MODE_FILLED) != 0) * 10);
  476.             ((struct rxd_waitevent *) *(&RexxState.array))->res.type    = &lToolType; 
  477.             ((struct rxd_waitevent *) *(&RexxState.array))->res.code1   = &lToolIndex;
  478.             SetStandardRexxReturns();
  479.         }
  480.         return;
  481.     }            
  482.  
  483.     /* If we get here, then we have a palette change */
  484.         nY = YPos - (DrawWindow->BorderTop + PState.nToolBoxHeight);
  485.     nX = XPos - nToolBoxLeft;
  486.     XSelectIndex = -nPaletteColumns-1;  /* no, really! */
  487.  
  488.     nPaletteSquareHeight = nGetPaletteSquareHeight();
  489.     if (nPaletteSquareHeight < 0) return;
  490.     nPaletteSquareWidth = nGetPaletteSquareWidth();
  491.     if (nPaletteSquareWidth < 0) return;
  492.  
  493.     while (nY > 0)
  494.     {
  495.          nY -= nPaletteSquareHeight;
  496.          XSelectIndex += nPaletteColumns;
  497.     }
  498.     while (nX > 0)
  499.     {
  500.         nX -= nPaletteSquareWidth;
  501.         XSelectIndex++;
  502.     }
  503.     
  504.     DrawPaletteEntry(PState.uwFColor, FALSE);
  505.     PState.uwFColor = XSelectIndex;
  506.  
  507.     if (nOptForceToolIndex >= 100)
  508.     {
  509.         nOptForceToolIndex -= 100;
  510.         if (nOptForceToolIndex < (1<<PState.ubDepth))
  511.                 PState.uwFColor = nOptForceToolIndex;
  512.     }
  513.     
  514.     DrawPaletteEntry(PState.uwFColor, TRUE);
  515.     
  516.     /* Tell ARexx about if he wants to know */
  517.     if (PState.uwRexxWaitMask & REXX_REPLY_COLORSELECT)
  518.     {
  519.         lToolIndex = XSelectIndex + (((PState.uwMode & MODE_FILLED) != 0) * 10);
  520.         ((struct rxd_waitevent *) *(&RexxState.array))->res.type    = &lColorType; 
  521.         ((struct rxd_waitevent *) *(&RexxState.array))->res.code1   = &lToolIndex;
  522.         SetStandardRexxReturns();
  523.     }
  524.     return;
  525. }
  526.  
  527.  
  528.